Use GtkApplication in the examples
authorEmmanuele Bassi <ebassi@gnome.org>
Wed, 21 Oct 2020 21:44:53 +0000 (22:44 +0100)
committerEmmanuele Bassi <ebassi@gnome.org>
Wed, 21 Oct 2020 21:44:53 +0000 (22:44 +0100)
Some people read the "Getting Started" section as a series of
incremental lessons, and having the examples go from GtkApplication to
the old style "init / spin the main loop" confuses them.

We should be using GtkApplication everywhere in our examples.

docs/reference/gtk/getting_started.md
examples/builder.c

index be40e5b05db43f8cf95d4986e6aa8144e8949ec6..c2f3057f0d254948c521b0364ea2f14a05191f22 100644 (file)
@@ -586,53 +586,53 @@ print_hello (GtkWidget *widget,
 }
 
 static void
-quit_cb (GtkWidget *widget, gpointer data)
+quit_cb (GtkWindow *window)
 {
-  gboolean *done = data;
-
-  *done = TRUE;
-
-  g_main_context_wakeup (NULL);
+  gtk_window_close (window);
 }
 
-int
-main (int   argc,
-      char *argv[])
+static void
+activate (GtkApplication *app,
+          gpointer        user_data)
 {
-  GtkBuilder *builder;
-  GObject *window;
-  GObject *button;
-  gboolean done = FALSE;
-
-#ifdef GTK_SRCDIR
-  g_chdir (GTK_SRCDIR);
-#endif
-
-  gtk_init ();
-
   /* Construct a GtkBuilder instance and load our UI description */
-  builder = gtk_builder_new ();
+  GtkBuilder *builder = gtk_builder_new ();
   gtk_builder_add_from_file (builder, "builder.ui", NULL);
 
   /* Connect signal handlers to the constructed widgets. */
-  window = gtk_builder_get_object (builder, "window");
-  g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
+  GObject *window = gtk_builder_get_object (builder, "window");
+  gtk_window_set_application (GTK_WINDOW (window), app);
 
-  button = gtk_builder_get_object (builder, "button1");
+  GObject *button = gtk_builder_get_object (builder, "button1");
   g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
 
   button = gtk_builder_get_object (builder, "button2");
   g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
 
   button = gtk_builder_get_object (builder, "quit");
-  g_signal_connect (button, "clicked", G_CALLBACK (quit_cb), &done);
+  g_signal_connect_swapped (button, "clicked", G_CALLBACK (quit_cb), window);
 
   gtk_widget_show (GTK_WIDGET (window));
 
-  while (!done)
-    g_main_context_iteration (NULL, TRUE);
+  /* We do not need the builder any more */
+  g_object_unref (builder);
+}
 
-  return 0;
+int
+main (int   argc,
+      char *argv[])
+{
+#ifdef GTK_SRCDIR
+  g_chdir (GTK_SRCDIR);
+#endif
+
+  GtkApplication *app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
+  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
+
+  int status = g_application_run (G_APPLICATION (app), argc, argv);
+  g_object_unref (app);
+
+  return status;
 }
 ```
 
index 261a099697c15a806d14bd29be50b7137c4cf037..5a93c974aeafd9419373a32a410c1f42d436a3d7 100644 (file)
@@ -11,49 +11,49 @@ print_hello (GtkWidget *widget,
 static void
 quit_cb (GtkWidget *widget, gpointer data)
 {
-  gboolean *done = data;
+  GtkWindow *window = data;
 
-  *done = TRUE;
-
-  g_main_context_wakeup (NULL);
+  gtk_window_close (window);
 }
 
-int
-main (int   argc,
-      char *argv[])
+static void
+activate (GtkApplication *app,
+          gpointer        user_data)
 {
-  GtkBuilder *builder;
-  GObject *window;
-  GObject *button;
-  gboolean done = FALSE;
-
-#ifdef GTK_SRCDIR
-  g_chdir (GTK_SRCDIR);
-#endif
-
-  gtk_init ();
-
   /* Construct a GtkBuilder instance and load our UI description */
-  builder = gtk_builder_new ();
+  GtkBuilder *builder = gtk_builder_new ();
   gtk_builder_add_from_file (builder, "builder.ui", NULL);
 
   /* Connect signal handlers to the constructed widgets. */
-  window = gtk_builder_get_object (builder, "window");
-  g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
+  GObject *window = gtk_builder_get_object (builder, "window");
+  gtk_window_set_application (GTK_WINDOW (window), app);
 
-  button = gtk_builder_get_object (builder, "button1");
+  GObject *button = gtk_builder_get_object (builder, "button1");
   g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
 
   button = gtk_builder_get_object (builder, "button2");
   g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
 
   button = gtk_builder_get_object (builder, "quit");
-  g_signal_connect (button, "clicked", G_CALLBACK (quit_cb), &done);
+  g_signal_connect_swapped (button, "clicked", G_CALLBACK (quit_cb), window);
 
   gtk_widget_show (GTK_WIDGET (window));
+  g_object_unref (builder);
+}
+
+int
+main (int   argc,
+      char *argv[])
+{
+#ifdef GTK_SRCDIR
+  g_chdir (GTK_SRCDIR);
+#endif
+
+  GtkApplication *app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
+  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
 
-  while (!done)
-    g_main_context_iteration (NULL, TRUE);
+  int status = g_application_run (G_APPLICATION (app), argc, argv);
+  g_object_unref (app);
 
-  return 0;
+  return status;
 }